#include <iostream>
#include <vector>
#include <pthread.h>
#include <unistd.h>

using namespace std;

#define NUM 10

struct threadData
{
    string threadname;
};

string toHex(pthread_t tid)
{
    char buffer[128];
    snprintf(buffer, sizeof(buffer), "0x%lx", tid);
    return buffer;
}

void InitThreadData(threadData* td, int number)
{
    td->threadname = "thread-" + to_string(number);
}

void* threadRoutine(void* args)
{
    threadData* td = static_cast<threadData*>(args);
    int i = 0;
    while(i < 10)
    {
        cout << "pid: " << getpid() << ", tid: " << toHex(pthread_self()) << ", threadname: " << td->threadname << "i: " << toHex((pthread_t)i) << endl;
        sleep(1);
        i++;
    }
    delete td;
    return nullptr;
}

int main()
{
    vector<pthread_t> tids;
    for(int i = 0; i < NUM; i++)
    {
        pthread_t tid;
        threadData* td = new threadData();
        InitThreadData(td, i);
        pthread_create(&tid, nullptr, threadRoutine, td);
        tids.push_back(tid);
        sleep(1);
    }
    for(int i = 0; i < tids.size(); ++i)
    {
        pthread_join(tids[i], nullptr);
    }
}